package com.JobFind.DAO.impl;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.JobFind.DAO.IResumeDAO;
import com.JobFind.model.TbResume;

@Component
public class ResumeDAO implements IResumeDAO {
	private SessionFactory sessionFactory;

	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}


	public Integer addResume(TbResume resumeBean) throws Exception {
		Integer result = 1;
		try {
			Session session = sessionFactory.getCurrentSession();
			session.save(resumeBean);
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	public Integer updateResume(TbResume resumeBean) throws Exception {
		Integer result = 1;
		try {
			Session session = sessionFactory.getCurrentSession();
			session.update(resumeBean);
		} catch (Exception e) {
		  e.printStackTrace();
		  result = 0;
		} 
		return result;
	}

	
	public TbResume getResInfoByUserId(Integer userId) throws Exception {
		TbResume result = new TbResume();
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql = "from TbResume where tbUser.userId=?";
			Query query = session.createQuery(hql);
			query.setInteger(0, userId);
			result = (TbResume) query.list().get(0);
		} catch (Exception e) {
		  e.printStackTrace();
		  result =null;
		}
		return result;
	}

	public Integer getResumeIdByUserId(Integer userId) throws Exception {
		Integer result=0;
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql = "select resumeId from TbResume where tbUser.userId=?";
			Query query = session.createQuery(hql);
			query.setInteger(0, userId);
			result = Integer.parseInt(query.uniqueResult().toString());
		} catch (Exception e) {
		  e.printStackTrace();
		  result = 0;
		}
		return result;
	}

	public TbResume getResumeInfoByUserId(Integer userId) throws Exception {
		TbResume result = new TbResume();
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql = "from TbResume where tbUser.userId=?";
			Query query = session.createQuery(hql);
			query.setInteger(0, userId);
			result = (TbResume) query.list().get(0);
		} catch (Exception e) {
		  e.printStackTrace();
		  result =null;
		}
		return result;
	}

}
